package com.cy.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cy.pojo.Equipment;
import com.cy.pojo.User;
import com.cy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cy
 * @since 2021-11-21
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService service;


    @ResponseBody
    @PostMapping("/queryPage")
    public Map<String, Object> query(@RequestBody @Nullable Map<String, Object> mymap) {
        System.out.println(mymap);
        Integer pageNumber = null;
        if (!mymap.containsKey("pagenumber")) {
            pageNumber = 1;
        }else {
           pageNumber = (Integer) mymap.get("pagenumber");
        }
        HashMap<String, Object> map = new HashMap<>();
        List<User> list = service.queryPage(1);
        map.put("userlist", list);
        map.put("pagenumber",pageNumber);
        return map;
    }

    @ResponseBody
    @RequestMapping("/login")
    public Map<String, Object> login(@RequestBody User user) {
        HashMap<String, Object> map = new HashMap<>();
        User user1 = service.getOne(new QueryWrapper<User>()
                .eq("name", user.getName())
                .eq("password", user.getPassword())
        );
        if (user1 != null) {
            map.put("denglu", true);
            map.put("name", user.getName());
        } else {
            map.put("denglu", false);
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/insert")
    public Map<String, Object> insert(@RequestBody User user) {
        System.out.println(user);
        HashMap<String, Object> map = new HashMap<>();
        if (service.save(user)) {
            map.put("message", true);
        } else {
            map.put("message", false);
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/disable")
    public Map<String, Object> disable(@RequestBody Map<String, Object> mymap) {
        HashMap<String, Object> map = new HashMap<>();
        Integer id = (Integer) mymap.get("id");
        User user = new User(id,0);
        if (service.updateById(user)) {
            map.put("message", true);
        } else {
            map.put("message", false);
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/dimission")
    public Map<String, Object> dimission(@RequestBody Map<String, Object> mymap) {
        HashMap<String, Object> map = new HashMap<>();
        Integer id = (Integer) mymap.get("id");
        User user = new User(id,2);
        if (service.updateById(user)) {
            map.put("message", true);
        } else {
            map.put("message", false);
        }
        return map;
    }


    @ResponseBody
    @PostMapping("/update")
    public Map<String, Object> update(@RequestBody @Nullable User user ){

        System.out.println(user);
        HashMap<String, Object> map = new HashMap<>();
        if (service.updateById(user)) {
            map.put("message", true);
        } else {
            map.put("message", false);
        }
        return map;
    }


    @ResponseBody
    @PostMapping("/queryName")
    public Map<String, Object> queryByName(@RequestBody Map<String, Object> mymap) {
        String name = (String) mymap.get("name");
        HashMap<String, Object> map = new HashMap<>();
        List<User> list = service.queryPage(1,name);
        map.put("userlist", list);
        return map;
    }

}

